<template>
  <el-main>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/device' }"
        >设备管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/device/unbind' }"
        >库存设备
      </el-breadcrumb-item>
      <el-breadcrumb-item>批量注册设备</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="margin-top-20">
      <el-card shadow="never">
        <el-form
          :model="formData"
          :rules="rules"
          label-width="120px"
          ref="formData"
        >
          <el-form-item label="设备类型" prop="deviceType">
            <el-radio-group v-model="formData.deviceType">
              <el-radio
                :key="item.code"
                :label="item.code"
                v-for="item in device_type"
                >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="通信方式" prop="deviceSocket">
            <el-radio-group v-model="formData.deviceSocket">
              <el-radio
                :key="item.code"
                :label="item.code"
                v-for="item in device_socket"
                >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item class="m-input-400" label="设备波段" prop="waveLength">
            <el-input autocomplete="off" v-model="formData.waveLength">
              <template slot="append"
                >MHz
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="平台信息" prop="platformType">
            <el-radio-group v-model="formData.platformType">
              <el-radio
                :key="item.code"
                :label="item.code"
                v-for="item in platform_type"
                >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="所属企业" prop="companyId">
            <el-select
              placeholder="请选择所属企业"
              v-model="formData.companyId"
            >
              <el-option
                :key="item.id"
                :label="item.companyName"
                :value="item.id"
                v-for="item in companyList"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="展示WEB页面" prop="toWeb">
            <el-switch
              active-text="是"
              inactive-text="否"
              v-model="formData.toWeb"
            ></el-switch>
          </el-form-item>
        </el-form>
        <el-form label-width="120px">
          <el-form-item label="设备文件:">
            <el-upload
              :auto-upload="false"
              :data="formData"
              :file-list="fileList"
              :limit="1"
              :multiple="false"
              :on-success="handleSuccess"
              accept=".xls,.xlsx"
              action="/device/unbind/add/batch"
              ref="upload"
            >
              <el-button size="small" slot="trigger" type="success"
                >选取文件</el-button
              >
              <div class="el-upload__tip" slot="tip">请上传.xls、.xlsx文件</div>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitUpload" type="primary"
              >立即注册
            </el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-main>
</template>

<script>
export default {
  data() {
    return {
      fileList: [],
      formData: {
        deviceType: null,
        deviceSocket: null,
        waveLength: null,
        platformType: null,
        companyId: null,
        toWeb: false
      },
      rules: {
        deviceType: [
          { required: true, message: "请选择设备类型", trigger: "none" }
        ],
        deviceSocket: [
          { required: true, message: "请选择设备类型", trigger: "none" }
        ],
        platformType: [
          { required: true, message: "请选择设备类型", trigger: "none" }
        ],
        waveLength: [{ required: true, message: "请输入波段", trigger: "none" }]
      },
      device_type: [],
      device_socket: [],
      platform_type: [],
      companyList: []
    };
  },
  mounted() {
    this.load_device_type();
    this.load_device_socket();
    this.load_platform_type();
    this.companyOptions();
  },
  methods: {
    submitUpload() {
      this.$refs["formData"].validate(valid => {
        console.log(valid);
        if (valid) {
          this.$refs.upload.submit();
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.$router.push("/device/unbind");
    },
    load_device_type() {
      this.$ajax.support
        .device_type()
        .then(result => {
          if (result.code === 1) {
            this.device_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    load_device_socket() {
      this.$ajax.support
        .device_socket()
        .then(result => {
          if (result.code === 1) {
            this.device_socket = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    load_platform_type() {
      this.$ajax.support
        .platform_type()
        .then(result => {
          if (result.code === 1) {
            this.platform_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    companyOptions() {
      this.$ajax.company
        .options()
        .then(result => {
          if (result.code == 1) {
            this.companyList = result.data;
          } else {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    },
    handleSuccess(response) {
      console.log(response);
      if (response.code === 1) {
        this.$message({
          message: "批量注册成功",
          type: "success"
        });
        this.$router.push("/device/unbind");
      } else {
        this.$message({
          message: response.msg,
          type: "warning"
        });
      }
    }
  }
};
</script>
